package learn.spring.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
import org.springframework.aop.aspectj.SimpleAspectInstanceFactory;
import org.springframework.aop.aspectj.TypePatternClassFilter;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import java.lang.reflect.Method;

/**
 * @author suzheng
 */
@Configuration
@ComponentScan(basePackages = "learn.spring.web")
@EnableAspectJAutoProxy
@Slf4j
public class AopConfig {

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor() throws NoSuchMethodException {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPattern(".*");
        pointcut.setClassFilter(new TypePatternClassFilter("learn.spring.web.BeanTest*") );

       DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
       defaultPointcutAdvisor.setPointcut(pointcut);
       defaultPointcutAdvisor.setAdvice(new MethodBeforeAdvice() {
           @Override
           public void before(Method method, Object[] args, Object target) throws Throwable {
               System.out.println("before----"+method.getName());
           }
       });
       return defaultPointcutAdvisor;
    }


    public static class BeforeAdviceMethod{
        public void beforeMethod(String name){
            System.out.println(this.getClass().getCanonicalName());
        }
    }



}
